package com.zq.im.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.zq.im.R;
import com.zq.im.adapter.FriendListAdapter;
import com.zq.im.api.FriendsApi;
import com.zq.im.bean.BaseModel;
import com.zq.im.bean.FriendListBean;
import com.zq.im.retrofit.BaseCallBack;
import com.zq.im.retrofit.RetrofitManager;
import com.zq.im.room.dao.UserDao;
import com.zq.im.room.database.MyDataBase;
import com.zq.im.room.entity.User;
import com.zq.im.utils.SharePreferenceUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;

/**
 * Created by 张强 869518570@qq.com on 2020/8/25
 */
public class FriendListFragment extends BaseFragment {

    private static final String TAG = "FriendListFragment";
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    private List<FriendListBean> mMyFriends;
    private FriendListAdapter mAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.list_fragment_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1));
        mAdapter = new FriendListAdapter(mMyFriends, getContext());
        View headerView = LayoutInflater.from(getContext()).inflate(R.layout.friends_item, null, false);
        mAdapter.addHeaderView(headerView);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        getFriendList();
    }

    public void refreshFriendList() {
        getFriendList();
    }

    private void getFriendList() {
        FriendsApi friendsApi = RetrofitManager.getRetrofit().create(FriendsApi.class);
        String userId = SharePreferenceUtils.getString("userId");
        Call<BaseModel<List<FriendListBean>>> getFriendsCall = friendsApi.getFriends(userId);

        getFriendsCall.enqueue(new BaseCallBack<List<FriendListBean>>() {
            @Override
            public void onSuccess(List<FriendListBean> friends) {
                mMyFriends = friends;
                if (mMyFriends != null) {
                    mAdapter.setFriendsBeans(mMyFriends);
                    mAdapter.notifyDataSetChanged();
                }
                ArrayList<User> users = new ArrayList<>();
                //获取到后列表之后
                for (FriendListBean friend : friends) {
                    User user = new User();
                    user.setId(friend.getFriendUserId());
                    user.setFaceImageBig(friend.getFriendFaceImage());
                    user.setNickName(friend.getFriendNickname());
                    user.setUserName(friend.getFriendUsername());
                    users.add(user);
                }

                UserDao userDao = MyDataBase.getInstance(mActivity).getUserDao();
                Disposable subscribe = userDao.insertAll(users)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(() -> {
                            Log.d(TAG, "插入成功");
                        }, throwable -> {
                            Log.d(TAG, "插入成功");
                        });


            }

            @Override
            public void onFailure(Throwable t) {
                Toast.makeText(mActivity, t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }


}
